package com.msg.xml;

import java.util.ArrayList;
import java.util.List;

public class CompSeats_Manipulator extends Table_Manipulator {

	private List<List<String>> compSeatsData = new ArrayList<List<String>>();
	private List<CapacityRecord> records = null;
	
	
	public CompSeats_Manipulator(Property prop) {
		super(prop);
	}
	/**
	 * get the event code, ticket type and available seats data from XML file within the Capacity field
	 * @return
	 */
	@Override
	protected String getSpecificData() {
		//pinpoint the tag *** CAPACITY first
		XMLhandler.findNodeByTag("***       COMP SEATS");
		//move the cursor forward by three positions
		XMLhandler.moveCursorForward();
		XMLhandler.moveCursorForward();
		XMLhandler.moveCursorForward();
		while(true) {
			ArrayList<String> contents = XMLhandler.getDataList();
			//reach the end, terminate the iteration
			if(contents.get(0).compareTo("TOTAL") == 0)
				break;
			//jump over the blank line
			if(contents.get(0).compareTo("") == 0)
				continue;
			//only read the rows that event codes end with (TOTAL)
			if(contents.get(0).endsWith("(TOTAL)")) {
				List<String> dataRow = new ArrayList<String>();
				String eventCode = contents.get(0);
				eventCode = eventCode.substring(0, eventCode.indexOf(" (TOTAL)")-1);
				dataRow.add(eventCode);
				dataRow.add(TableUtility.getTicketType(eventCode));
				for(int i = 4; i < contents.size(); i++) {
					if(contents.get(i).compareTo("") == 0)
						break;
					dataRow.add(contents.get(i));
				}
				compSeatsData.add(dataRow);
			}
		}
		return null;
	}

	/**
	 * find a capacity record in the given list of capacity records
	 * @param eventCode
	 * @param ticketType
	 * @param ticketLv
	 * @return, the capacity record if all parameters match, or return null
	 */
	private CapacityRecord findRecord(String eventCode, String ticketType, char ticketLv) {
		for(int i = 0; i < records.size(); i++) {
			CapacityRecord record = records.get(i);
			if(record.getEventCode().compareTo(eventCode) != 0)
				i += prop.getTICKETLEVEL_NUM() - 1;
			else if((record.getTicketType().compareTo(ticketType) == 0) && (record.getTicketLevel() == ticketLv))
				return record;
		}
		return null;
	}
	
	/**
	 * transform the raw data of comp seats read from XML to the data in CapacityRecords
	 * @return
	 */
	@Override
	protected String update() {
		for(int i = 0; i < compSeatsData.size(); i++) {
			List<String> rowData = compSeatsData.get(i);
			for(int j = 0; j < prop.getTICKETLEVEL_NUM(); j++) {
				CapacityRecord record = findRecord(rowData.get(0), rowData.get(1), (char)('A' + j));
				//only the record already exist will be used, its available seats column will be set.
				if(record != null) {
					record.setCompSeats(rowData.get(2 + j));
				}
			}
		}
		return null;
	}

	/**
	 * give an capacity records list with capacity data already exist
	 * @param o, a list of capacity records
	 */
	@Override
	@SuppressWarnings("unchecked")
	protected void pushData(Object o) {
		records = (List<CapacityRecord>)o;
	}

}
